<template>
  <!-- 放置一个图标 -->
  <div>
    <svg-icon
      :icon-class="isFull ? 'exit-fullscreen' : 'fullscreen'"
      class="fullscreen"
      @click="toggleScreen"
    />
  </div>
</template>

<script>
// 导入全屏
import screenfull from 'screenfull'

export default {
  name: 'ScreenFull',
  data () {
    return {
      isFull: false // 是否是全屏
    }
  },
  created () { this.onScreen() },
  beforeDestroy () {
    // 解绑事件
    screenfull.off('change')
  },
  methods: {
    // 监控浏览器全屏状态
    // 语法 screenfull.on('change',callback)
    onScreen () {
      screenfull.on('change', () => {
        if (screenfull.isFullscreen) {
          this.isFull = true
        } else {
          this.isFull = false
        }
      })
    },
    // 开启或关闭全屏
    toggleScreen () {
      /**
 * 需求 : 点击icon图标切换全屏
 * 使用技术 : 选择screenfull 插件 浏览器兼容判断 提供了好用简介的方法
 * 1. 判断兼容问题 => sf.isEnable 返回true
 */
      if (!screenfull.isEnabled) {
        return this.$message.error('浏览器不支持全屏功能!')
      }
      screenfull.toggle()
    }
  }
}
</script>

<style lang="scss" scoped>
.fullscreen {
  width: 20px;
  height: 20px;
  color: #fff;
  cursor: pointer;
}
</style>
